/**
 * 
 */
package com.antilia.infomaresme.pages.myitems;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;

import com.antilia.infomaresme.InfomaresmeSession;
import com.antilia.infomaresme.data.dataobjects.Category;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.data.dataobjects.User;
import com.antilia.infomaresme.data.dataobjects.VisibilityStatus;
import com.antilia.infomaresme.pages.RoundButtonLinkPanel;
import com.antilia.infomaresme.pages.details.DetailsPanel;
import com.antilia.infomaresme.pages.edit.EditItemPage;
import com.antilia.infomaresme.pages.edit.news.EditNewsPage;
import com.antilia.infomaresme.panels.filter.CategoryFilterPanel;
import com.antilia.infomaresme.provider.ItemsDataProvider;
import com.antilia.infomaresme.provider.ItemsLdm;
import com.antilia.infomaresme.resources.InfoModalWindow;

/**
 * @author Ernesto
 *
 */
public class ListItemsPanel extends Panel {

	private static final long serialVersionUID = 1L;

	private ItemsDataProvider dataProvider;
	
	private AjaxFallbackDefaultDataTable<Item> items;
	
	private ModalWindow modal;
	
	private CategoryFilterPanel filter;
	/**
	 * @param id
	 */
	public ListItemsPanel(String id, IModel<User> model) {
		super(id, model);
		modal = new InfoModalWindow("modal");
		modal.setTitle("Detalles");
		modal.setInitialHeight(640);
		modal.setInitialWidth(950);
		modal.setResizable(false);
		add(modal);
		
		filter = new CategoryFilterPanel("filter") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onCategorySelected(AjaxRequestTarget target,
					Category category, Category place) {
				target.add(items);
			}
		};
		add(filter);
		List<IColumn<Item>> columns = new ArrayList<IColumn<Item>>();	
		columns.add(new PropertyColumn<Item>(Model.of(""), null , "price") {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void populateItem(
					org.apache.wicket.markup.repeater.Item<ICellPopulator<Item>> item,
					String componentId, final IModel<Item> rowModel) {
				
				DetailsLinksPanel linksPanel = new DetailsLinksPanel(componentId, new ItemsLdm(rowModel.getObject())){
					
					/**
					 * 
					 */
					private static final long serialVersionUID = 1L;

					@Override
					public void onSee(AjaxRequestTarget target, IModel<Item> model) {
						modal.setTitle(new ResourceModel("itemDetails"));
						modal.setContent(new DetailsPanel(modal.getContentId(),model));
						modal.show(target);
					}
					
					@Override
					public void onEdit(AjaxRequestTarget target, IModel<Item> model) {
						setResponsePage(new EditItemPage(model));						
					}
					
					@Override
					public void onSeeNews(AjaxRequestTarget target,
							IModel<Item> model) {
						setResponsePage(new EditNewsPage(model));	
					}
				};
				item.add(linksPanel);
			}
		});
		columns.add(new PropertyColumn<Item>(new ResourceModel("description"), null, "title"));
		columns.add(new PropertyColumn<Item>(new ResourceModel("location"), null, "geographicLocation"));
		columns.add(new PropertyColumn<Item>(new ResourceModel("status"), null, "status"));
		columns.add(new PropertyColumn<Item>(new ResourceModel("price"), "price" , "price"));
		
		
		
 		dataProvider = new ItemsDataProvider() {
 			
 			private static final long serialVersionUID = 1L;

			@Override
 			public Category getCategory() {
 				return filter.getSelected();
 			}
 			
 			@Override
 			public Category getPlace() {
 				return filter.getPlace();
 			}
 			
 			public User getUser() {
 				return InfomaresmeSession.getSession().getUser();
 			};
 			
 			@Override
 			protected VisibilityStatus getItemStatus() {
 				// return all items.
 				return null;
 			}
 		};		
		items  = new AjaxFallbackDefaultDataTable<Item>("items", columns, dataProvider, 8);
		items.setOutputMarkupId(true);
		addOrReplace(items);
		
		addOrReplace(new RoundButtonLinkPanel("create", new ResourceModel("actions.createNewItem")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				setResponsePage(new EditItemPage(new Item()));
			}
		});
	}

}
